package com.hs.pre.init;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hs.HsBase;
import com.hs.core.action.BaseAction;
import com.hs.log.HsLogger;
import com.hs.pre.SearchAction;
import com.hs.pre.db.InitDatabaseData;
import com.hs.pre.model.PreEntry;
import com.hs.pre.model.SortBean;
import com.hs.pre.module.sort.InitSortData;

public class InitDataAction extends BaseAction{
	private static final HsLogger logger = HsLogger.getLogger(SearchAction.class.getName());
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception { 
		InitSortData init = new InitSortData();
		boolean flag = init.isExsit(HsBase.getAnsiSqlMap().get("presort.count")+"");
		System.out.println("分类表中是否存在数据:" + flag);
		//init.init(HsBase.getAnsiSqlMap().get("presort.init")+"");
		if(flag){
			init.init(HsBase.getAnsiSqlMap().get("presort.delete")+"");
			System.out.println("删除分类表中数据完成");
		}
		init.init(HsBase.getAnsiSqlMap().get("presort.init")+"");
		int laiy = init.selectByIsRoot("-3");
		int jixing = init.selectByIsRoot("-2");
		System.out.println("laiy:" + laiy);
		System.out.println("jixing:" + jixing);
		InitDatabaseData fangjiinit = new InitDatabaseData();
		List preList = fangjiinit.initData(1, true);
//		System.out.println("preList:" + preList);
		if(null != preList){
			for(int i=0;i<preList.size();i++){
				PreEntry entry = (PreEntry)preList.get(i);
				//插入方剂表并返回方剂表主键
				int fangjiId = Integer.parseInt(fangjiinit.insertPre(HsBase.getAnsiSqlMap().get("preinfo.insert")+"", entry));
				//判断当前方剂有多少个来源，并按来源名称查找是否存在该来源，如存在则返回分类主键
				//不存在则插入返回主键 
				List lylist = entry.getFjly();
				System.out.println("来源:" + lylist+ "entry:" + entry.getFangjlb());
				if(null != lylist){
					int parentId = laiy; 
					for(int j=0;j<lylist.size();j++){
						SortBean bean = new SortBean();
						bean.setParentSortId(parentId);
						bean.setSortName(lylist.get(j)+""); 
						String sortId = init.isExsitByName(bean,HsBase.getAnsiSqlMap().get("presort.selectbyName")+"",HsBase.getAnsiSqlMap().get("presort.insert")+"");
						//插入方剂与分类关联关系表
						init.newRelation(Integer.parseInt(sortId), fangjiId, 1, HsBase.getAnsiSqlMap().get("presortrelation.insert")+"");
					}
				}
				String preType = entry.getFangjlb();
				if(null != preType){
					int pId = jixing; 
					SortBean bean = new SortBean();
					bean.setParentSortId(pId);
					bean.setSortName(preType);
					String sortId = init.isExsitByName(bean,HsBase.getAnsiSqlMap().get("presort.selectbyName")+"",HsBase.getAnsiSqlMap().get("presort.insert")+"");
					//插入方剂与分类关联关系表
					init.newRelation(Integer.parseInt(sortId), fangjiId, 1, HsBase.getAnsiSqlMap().get("presortrelation.insert")+"");
				}
				
			}
		}
		return null;
	}
}
